package ca.uwindsor;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class WelcomeMenu extends ListActivity implements OnItemClickListener {

	String term = "Add New Term";
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		//fullscreen
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		
		//start notifications
		 Intent i=new Intent("ca.uwindsor.NOTIFICATIONSERVICE");
		startService(i);
		
		ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(
				this, android.R.layout.simple_list_item_1);
		adapter.add(term);

		// TODO:
		// add ur list to adapter using adapter.add() from database.....
		// test do remove this line use database to populate
		
		//Populate the list with the semesters from the database.
		final MyDBAdapter db = new MyDBAdapter(this);
		db.setDATABASE_TABLE("semester");
		db.open();
		
		Cursor ret = db.getAllEntries();
		if(ret.getCount()>0){
			ret.moveToFirst();
			do{
				adapter.add(ret.getString(1));
				ret.moveToNext();
			}while(!ret.isAfterLast());
		}
		
		db.close();
		setListAdapter(adapter);
		ListView lv = getListView();
		lv.setTextFilterEnabled(true);
		lv.setOnItemClickListener(this);

	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		//finish();
	}

	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		if (parent.getItemAtPosition(position).toString().equals(term)) {
			startActivity(new Intent("ca.uwindsor.ADDSEMESTER"));
			finish();
		} else {
				//TODO:
			// set the global string to semester here before u go to new activity....
			Common.globalTerm = parent.getItemAtPosition(position).toString();
			startActivity(new Intent("ca.uwindsor.SUMMARY"));
		}

	}

}
